﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XMLFT;

namespace TemplateViewer
{
    public partial class tab1UserControl : UserControl
    {
        public tab1UserControl()
        {
            InitializeComponent();
        }

        #region Helper Functions

        public void LoadDefaultTemplate()
        {
            // If you want to load a specific template at control load, do it here. Call this from the main form.
            XMLFormTemplate defaultTemplate = new XMLFormTemplate();
            defaultTemplate.LoadXMLTemplate(Program.genForms.FormsPath + "General.xml", scInTab1.Panel1);
        }

        private void ToggleIncidentFormButtons(bool state)
        {
            btnRefresh.Enabled = state;
            btnLoad.Enabled = state;

        }

        public void ResetIncidentFormComboBox()
        {
            ToggleIncidentFormButtons(false);

            try
            {
                cbIncidentForms.DataSource = null;

                DataSet dsSet = new DataSet();
                dsSet.ReadXml(Program.genForms.FormsList);
                cbIncidentForms.DataSource = dsSet.Tables["form"];
                cbIncidentForms.DisplayMember = "display";
                cbIncidentForms.ValueMember = "name";
                cbIncidentForms.SelectedIndex = 0;
            }
            catch (Exception exp)
            {
                MessageBox.Show("Error reading list of General Forms. Try again later." + Environment.NewLine + exp.Message);
            }

            ToggleIncidentFormButtons(true);
        }

        #endregion

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            ResetIncidentFormComboBox();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (cbIncidentForms.SelectedIndex == 0)
            {
                return;
            }

            ToggleIncidentFormButtons(false);

            do
            {
                foreach (Control child in scInTab1.Panel1.Controls)
                {
                    scInTab1.Panel1.Controls.Remove(child);
                    child.Parent = null;
                    child.Dispose();
                }
            } while (scInTab1.Panel1.Controls.Count > 0);

            scInTab1.Panel1.Controls.Clear();
            scInTab1.Panel1.Update();

            XMLFormTemplate theTemplate = new XMLFormTemplate();
            theTemplate.LoadXMLTemplate((Program.genForms.FormsPath + cbIncidentForms.SelectedValue.ToString() + ".xml"), scInTab1.Panel1);

            ToggleIncidentFormButtons(true);
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Reset Your Form?", "Reset?", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }
            XMLFormHelper.resetFormContainer(scInTab1.Panel1);
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            XMLFormHelper.copyFormToClipboard(scInTab1.Panel1);
        }



    }
}
